package com.cjm.algorithm.leetcode;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class NoRepterStr {
    public static int solution(String s) {
        char[] x = {'a', 'a'};
        if (s == null || s.length() < 1) {
            return 0;
        }
        // 贪心算法
        int max = 1;
        int left = 0;
        char[] chars = s.toCharArray();
        Queue queu = new LinkedBlockingQueue(s.length());
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (map.containsKey(String.valueOf(aChar))) {

                boolean flag = true;
                while (flag) {
                    Object poll =  queu.poll();
                    if (poll == null || (char)poll == aChar) {
                        flag = false;
                    } else {
                        map.remove(poll+"");
                    }
                }
                Integer remove = map.remove(aChar+"");
                left = remove + 1;
                queu.add(aChar);
                map.put(aChar+"", i);
            } else {
                map.put(aChar+"", i);
                queu.add(aChar);
                max = Math.max(max, (i - left) + 1);
            }
        }
        return max;
    }

    public static void main(String[] args) {
        System.err.println(solution("tmmzuxt"));
        System.err.println(solution("tbtaz"));
        System.err.println(solution("abba"));
    }
}
